<html>
<head>
<script>

var ws = null;
var msg_id = 0;

function connect() {
    if (ws !== null) return log('already connected');
    ws = new WebSocket('ws://115.28.88.113:8088/test');
    //115.28.88.113, cheeray-aliyun
    //128.199.140.15:8088, cheeray-do
    ws.onopen = function () {
        log('connected');
    };
    ws.onerror = function (error) {
        log(error);
    };
    ws.onmessage = function (e) {
        log('recv: ' + e.data);
    };
    ws.onclose = function () {
        log('disconnected');
        ws = null;
    };
    return false;
}

function disconnect() {
    if (ws === null) return log('already disconnected');
    ws.close();
    return false;
}


function GetCodes() {
    if (ws === null) return log('please connect first');
    
    var data = {
        "_msgid": msg_id, 
        "action": "user.uploadCoDEs", 
        "commit" : "29c1",
        "session_id": "397a20a4e4490badf26018bc70bc18f9"
    }

    var jsonData = JSON.stringify(data)
        ws.send(jsonData);
    msg_id = msg_id + 1

        log('Uploadcodes: ' + jsonData);

    return false;
}

function SayHello() {
    if (ws === null) return log('please connect first');
    
    var data = {
        "_msgid": msg_id, 
        "action": "Say.saYHello", 
        "user_def_mod": "module1", 
        "name" : "a_name_via_ws",
        "session_id": "397a20a4e4490badf26018bc70bc18f9"
    }

    var jsonData = JSON.stringify(data)
        ws.send(jsonData);
    msg_id = msg_id + 1

        log('SayHello: ' + jsonData);

    return false;
}

function log(text) {
    var li = document.createElement('li');
    li.appendChild(document.createTextNode(text));
    document.getElementById('log').appendChild(li);
    return false;
}

</script>

</head>
<body>
<form onsubmit="return SayHello();">
    <button type="button" onclick="return connect();">
        Connect
    </button>
    <button type="button" onclick="return disconnect();">
        Disconnect
    </button>
    <button type="button" onclick="return GetCodes();">
        GetCodes 
    </button>
    <button type="submit">Say Hello</button>
</form>

<ol id="log"></ol>
</body>
</html>
